package metier.sess;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import metier.entities.categorie;

@Stateless(name="CAt")
public class CategorieEJBImpl implements ICategorieLocal{

	@PersistenceContext(unitName="PU_Cargaison")
	private EntityManager em;

	@Override
	public Integer addCategorie(categorie c) {
		em.persist(c);
		return c.getIdCategorie();
	}
	@Override
	public List<categorie> getAllCategories() {
		Query req=em.createQuery("select c from categorie c");
		return req.getResultList();
	}
	
	
	
	@Override
	public List<categorie> getAllCategoriesActive() {
		Query req=em.createQuery("select c from categorie c where c.Status=true");
		return req.getResultList();
	}

	@Override
	public categorie getCategorie(Integer Id) {
		categorie cat=em.find(categorie.class, Id);
		if(cat==null)throw new RuntimeException("compte introuvable");
		return cat;
	}

	@Override
	public void updateCategorie(categorie c) {
		em.merge(c);
		
	}

	@Override
	public void deleteCategorie(Integer Id) {
		categorie ct=getCategorie(Id);
		em.remove(ct);
		
	}







	
	
	

}
